package com.grablist.feihu.grablist.base;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.trello.rxlifecycle.components.support.RxFragment;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;

import com.grablist.feihu.grablist.comm.Constants;
import com.grablist.feihu.grablist.event.NetErrorEvent;
import com.grablist.feihu.grablist.util.AlertUtils;
import com.grablist.feihu.grablist.util.ToastUtils;

/**
 * Created by JINAN on 2017/10/12.
 */

public abstract class BaseFragment extends RxFragment  {


    private AlertDialog loading;
    private int retryCount;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }




    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            onResume();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }



    public void startActString(Class clz, String data) {
        Intent intent = new Intent(getContext(), clz);
        if (data != null && !"".equals(data)) {
            intent.putExtra(Constants.KEYSTRING, data);
        }
        startActivity(intent);
    }

    public void startAct(Intent intent) {
        startActivity(intent);
    }

    public void startAct(Class clz, Serializable serializable) {
        Intent intent = new Intent(getContext(), clz);
        if (serializable != null) {
            intent.putExtra(Constants.KEY, serializable);
        }
        startActivity(intent);
    }

    public void finishAct() {
        getActivity().finish();
    }

    /**
     * 结束所有不含主页面
     */

    public void finishAllAct() {
        if (Constants.activitys.size() > 0) {
            for (Activity act :
                    Constants.activitys) {
                act.finish();

            }
            Constants.activitys.clear();
        }

    }

    public void showMsg(String msg) {
        ToastUtils.shortToast(msg);
    }


    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onNetErrorEvent(NetErrorEvent event) {
        if (event.getLevel() > 0) { // 重连策略
            if (retryCount < 3) { // 限制3次
                onNetError(event);
                retryCount++;
            } else {
                loading(false);
                retryCount = 0;
            }
        } else {
            loading(false);
        }
    }

    protected abstract void onNetError(NetErrorEvent event);

    public void loading(boolean isLoading) {
        if (isLoading) {
            if (loading == null) {
                loading = AlertUtils.loadingDialog(getContext());
            }
            if (!loading.isShowing()) {
                loading.show();
            }
        } else {
            if (loading != null && loading.isShowing()) {
                loading.dismiss();
            }
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getActivity());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getActivity());
    }

    /**
     * 结束注册登录
     */

    public void finishRegistetAct() {
        if (Constants.registerActivity.size() > 0) {
            for (Activity act :
                    Constants.registerActivity) {
                act.finish();

            }
            Constants.registerActivity.clear();
        }

    }
}
